<template>
  <div>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="getList(1)"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="searchList"
          size="small"
          label-width="140px"
        >
          <el-form-item label="开始时间:">
            <el-date-picker
              v-model="searchList.startTime"
              type="date"
              placeholder="选择开始时间"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item label="结束时间:">
            <el-date-picker
              v-model="searchList.endTime"
              type="date"
              placeholder="选择结束时间"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="订单状态:">
            <el-select v-model="searchList.status" placeholder="请选择">
              <el-option
                v-for="item in statusoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-dialog title="评论区" :visible.sync="show" width="50%">
      <span
        ><el-card class="filter-container" shadow="never">
          <div v-if="comment != null">
            <el-row>
              <el-col :span="2"
                ><div class="grid-content bg-purple">
                  <el-avatar
                    src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
                  ></el-avatar></div
              ></el-col>
              <el-col :span="12"
                ><div class="grid-content bg-purple-light">
                  <h3>匿名用户</h3>
                  <el-row>
                    <el-col :span="12"
                      ><div class="grid-content bg-purple">
                        <p style="margin-top: 5px">
                          {{ comment.createTime }}
                        </p>
                      </div></el-col
                    >
                    <el-col :span="12"
                      ><div class="grid-content bg-purple-light">
                        <el-rate
                          style="margin-top: 5px"
                          v-model="comment.score"
                          disabled
                          show-text
                        ></el-rate></div
                    ></el-col>
                  </el-row></div
              ></el-col>
            </el-row>
            <div style="margin-left: 57px">
              <el-card
                style="background-color: #f0f4fd"
                class="filter-container"
                shadow="never"
              >
                {{ comment.comments }}
              </el-card>
            </div>
            <el-divider></el-divider>
          </div>
          <div v-if="comment == null">
            <el-empty description="暂无评论"></el-empty>
          </div> </el-card
      ></span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="show = false">返 回</el-button>
      </span>
    </el-dialog>
    <el-table
      v-loading="Loading"
      :data="tableData"
      border
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="orderId" label="订单号" width="150" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="
              showComment(scope.row.orderId);
              show = true;
            "
            >{{ scope.row.orderId }}</el-button
          ></template
        >
      </el-table-column>
      <el-table-column
        prop="companyName"
        label="公司名"
        width="180"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="description"
        label="描述"
        width="180"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="address"
        label="服务地址"
        width="180"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180" align="center">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="订单创建时间"
        width="180"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="startTime"
        label="开始服务时间"
        width="180"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="price" label="金额" width="180" align="center">
      </el-table-column>
      <el-table-column prop="qty" label="人数" width="100" align="center">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 'DISPATCH'">已分配人员</el-tag>
          <el-tag type="warning" v-else-if="scope.row.status == 'REFUND'"
            >已退款</el-tag
          >
          <el-tag type="danger" v-else-if="scope.row.status == 'CANCEL'"
            >已取消</el-tag
          >
          <el-tag type="info" v-else-if="scope.row.status == 'ORDER'"
            >已下单</el-tag
          >
          <el-tag v-else-if="scope.row.status == 'GOING'">进行中</el-tag>
          <el-tag v-else-if="scope.row.status == 'PAYING'">待支付</el-tag>
          <el-tag type="success" v-else>已结束</el-tag></template
        >
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 'PAYING'"
            size="mini"
            type="primary"
            @click="
              pay(scope.row.orderId)
            "
            >支付
          </el-button>
          <el-button
            v-if="
              scope.row.status == 'ENDED' && scope.row.commentStatus == 'NO'
            "
            size="mini"
            type="success"
            @click="addcomment(scope.row.orderId, scope.row.companyId)"
            >评价
          </el-button>
          <el-button
            :disabled="
              scope.row.status != 'ORDER'
            "
            size="mini"
            type="warning"
            @click="cancel(scope.row.orderId)"
            >取消
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加评论" :visible.sync="showaddcomment" width="40%">
      <span
        ><el-card class="filter-container" shadow="never">
          <el-form
            :rules="rules"
            ref="addData"
            :model="addData"
            label-width="80px"
          >
            <el-form-item label="评价:" prop="comments">
              <el-input v-model="addData.comments"></el-input>
            </el-form-item>
            <el-form-item label="评分:" prop="score">
              <el-rate
                style="margin-top: 12px"
                v-model="addData.score"
                show-text
              >
              </el-rate>
            </el-form-item> </el-form></el-card
      ></span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showaddcomment = false">取 消</el-button>
        <el-button type="primary" @click="sendcomment()">确 定</el-button>
      </span>
    </el-dialog>
    <div class="block">
      <el-pagination
        @current-change="getList"
        layout="prev, pager, next"
        :page-count="pageCunt"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "adminHome",
  data() {
    return {
      searchList: {
        startTime: "",
        endTime: "",
        status: "",
        pageNum: "",
        pageSize: 5,
      },
      pageCunt: 1,
      currenPage: 1,
      tableData: [],
      addData: {},
      show: false,
      Loading: false,
      showaddcomment: false,
      comment: {},
      rules: {
        comments: [{ required: true, message: "请填写评价", trigger: "blur" }],
        score: [{ required: true, message: "请评分", trigger: "blur" }],
      },
      statusoptions: [
        {
          value: "DISPATCH",
          label: "已分配人员",
        },
        {
          value: "REFUND",
          label: "已退款",
        },
        {
          value: "CANCEL",
          label: "已取消",
        },
        {
          value: "ORDER",
          label: "已下单",
        },
        {
          value: "GOING",
          label: "进行中",
        },
        {
          value: "PAYING",
          label: "待支付",
        },
        {
          value: "ENDED",
          label: "已结束",
        },
      ],
    };
  },
  created() {
    this.getList(1);
  },
  methods: {
    getList(pageNum) {
      this.searchList.pageNum = pageNum;
      this.currenPage = pageNum;
      this.Loading = true;
      this.axios
        .get("api/user/auth/order/list", {
          params: this.searchList,
        })
        .then((response) => {
          this.tableData = response.data.value.data;
          if (
            response.data.value.total / 5 >
            Number.parseInt(response.data.value.total / 5)
          ) {
            this.pageCunt = Number.parseInt(response.data.value.total / 5) + 1;
          } else {
            this.pageCunt = Number.parseInt(response.data.value.total / 5);
          }
          this.Loading = false;
        })
        .catch((error) => {
          this.$message.error(error);
          this.Loading = false;
        });
    },
    cancel(orderId) {
      this.axios
        .get("api/user/auth/order/cancel?orderId=" + orderId)
        .then((response) => {
          console.log(response);
          if (response.data.code == "200") {
            this.$message({
              message: "取消成功",
              type: "success",
            });
            this.getList(this.currenPage);
          }
        })
        .catch((error) => {
          this.$message.error(error);
          this.Loading = false;
        });
    },
    handleResetSearch() {
      this.searchList = {
        startTime: "",
        endTime: "",
        status: "",
        pageNum: "",
        pageSize: 5,
      };
    },
    addcomment(orderId, companyId) {
      this.showaddcomment = true;
      this.addData.orderId = orderId;
      this.addData.companyId = companyId;
    },
    sendcomment() {
      this.$refs.addData.validate((valid) => {
        if (valid) {
          this.axios
            .post(
              "api/user/auth/comment/insert?comments=" +
                this.addData.comments +
                "&companyId=" +
                this.addData.companyId +
                "&orderId=" +
                this.addData.orderId +
                "&score=" +
                this.addData.score
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
                this.showaddcomment = false;
                this.getList(this.currenPage);
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error(error);
            });
        } else {
          this.$message.error("请填写全部内容");
          return false;
        }
      });
    },
    showComment(id) {
      this.axios
        .get("api/user/comment/selectByOrderId?orderId=" + id)
        .then((response) => {
          this.comment = response.data.value;
          this.comment.createTime =
            new Date(this.comment.createTime).getFullYear() +
            "-" +
            (new Date(this.comment.createTime).getMonth() + 1) +
            "-" +
            new Date(this.comment.createTime).getDate() +
            " " +
            new Date(this.comment.createTime).getHours() +
            ":" +
            new Date(this.comment.createTime).getMinutes() +
            ":" +
            new Date(this.comment.createTime).getSeconds();
        })
        .catch((error) => {});
    },
    pay(orderId) {
      this.$confirm('是否支付该订单?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'success'
        }).then(() => {
          this.axios
        .post(
          "api/user/auth/wallet/pay?&orderId=" + orderId)
        .then((response) => {
          if (response.data.code == "200") {
            this.$message({
              message: "支付成功",
              type: "success",
            });
            this.getList(this.currenPage)
          } else {
            this.$message.error(response.data.message);
          }
        })
        .catch((error) => {});
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消支付'
          });          
        });
      
    },
  },
};
</script>